﻿var jweixin = require('jweixin-module');
export default {
    //判断是否在微信中  
    isWechat: function () {
        var ua = window.navigator.userAgent.toLowerCase();
        if (ua.match(/micromessenger/i) == 'micromessenger') {
            // console.log('是微信客户端')
            return true;
        } else {
            // console.log('不是微信客户端')
            return false;
        }
    },
    //初始化sdk
    initJsSdk(callback) {
            let timer = setInterval(() => {
                let obj = sessionStorage.getItem('offObj');
                if (obj) {
                    obj = JSON.parse(obj);
                    clearInterval(timer)
                    jweixin.config({
                        debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                        appId: obj.js_appid, // 必填，公众号的唯一标识
                        timestamp: obj.timestamp, // 必填，生成签名的时间戳
                        nonceStr: obj.nonceStr, // 必填，生成签名的随机串
                        signature: obj.signature,// 必填，签名，见附录1
                        jsApiList: ["onMenuShareTimeline", "onMenuShareAppMessage", "onMenuShareQQ", "onMenuShareWeibo", "startRecord", "stopRecord", "onVoiceRecordEnd", "playVoice", "pauseVoice", "stopVoice", "onVoicePlayEnd", "uploadVoice", "downloadVoice", "chooseImage", "previewImage", "uploadImage", "downloadImage", "translateVoice", "getNetworkType", "openLocation", "getLocation", "hideOptionMenu", "showOptionMenu", "hideMenuItems", "showMenuItems", "hideAllNonBaseMenuItem", "showAllNonBaseMenuItem", "closeWindow", "scanQRCode", "chooseWXPay", "openProductSpecificView", "addCard", "chooseCard", "openCard"],  // 必填，需要使用的JS接口列表，所有JS接口列表见附录2


                    });
                    if (callback) {
                        callback(obj)
                    }
                }
            }, 1000)
       

    },
    /**
     * 
     * @param {*} shareData 分享参数
     */
    share(shareData) {
        this.initJsSdk(function () {
            jweixin.ready(() => {
                //分享给朋友接口  
                jweixin.onMenuShareAppMessage(shareData);
                //分享到朋友圈接口  
                jweixin.onMenuShareTimeline(shareData);
            })

        })
    },
    /**
     * 定位
     */
    getLocation() {
        return new Promise((reslove, reject) => {
            this.initJsSdk(function () {
                jweixin.ready(() => {
                    jweixin.getLocation({
                        success: function (res) {
                            reslove(res)
                        },
                        fail: function (res) {
                            reject(res)
                        }
                    });
                })

            })
        })
    },
    /**
     * sourceType相机相册
     */
    chooseImage(sourceType=['album', 'camera']){
        return new Promise((reslove, reject) => {
            this.initJsSdk(function () {
                jweixin.ready(() => {
                    jweixin.chooseImage({
                        count: 1, // 默认9
                        sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
                        sourceType: sourceType, // 可以指定来源是相册还是相机，默认二者都有
                        success: function (res) {
                            reslove(res.localIds)
                        },
                        fail: function (fail) {
                            reject()
                        }
                    });
                })

            })
        })
    },
    /**
     * 
     * @param {*} localId 本地id
     */
    wxUploadImage(localId){
        return new Promise((reslove, reject) => {
            this.initJsSdk(function () {
                jweixin.ready(() => {
                    jweixin.uploadImage({
                        localId: localId[0], // 需要上传的图片的本地ID，由chooseImage接口获得
                        isShowProgressTips: 1, // 默认为1，显示进度提示
                        success: function (res) {
                            reslove(res.serverId)
                        },
                        fail: function (res) {
                            reject()
                        }
                    });
                })

            })
        })
    }
}